package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.CollectVideoActivity;
import flc.ast.activity.LocalVideoActivity;
import flc.ast.activity.VideoAlbumActivity;
import flc.ast.adapter.VideoAlbumAdapter;
import flc.ast.bean.MyAlbumBean;
import flc.ast.databinding.FragmentVideoAlbumBinding;
import flc.ast.dialog.CollectEditDialog;
import flc.ast.dialog.RenameDialog;
import flc.ast.utils.AppSpUtils;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link VideoAlbumFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class VideoAlbumFragment extends BaseNoModelFragment<FragmentVideoAlbumBinding> {

    private VideoAlbumAdapter albumAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_video_album;
    }

    @Override
    protected void initView() {
        mDataBinding.rvAlbumList.setLayoutManager(new GridLayoutManager(mContext, 2));

        albumAdapter = new VideoAlbumAdapter();
        mDataBinding.rvAlbumList.setAdapter(albumAdapter);
        albumAdapter.setOnItemClickListener(this);

        mDataBinding.ivLocalVideoImg.setOnClickListener(this);
        mDataBinding.ivCollectVideoImg.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivLocalVideoImg://本地视频
                startActivity(LocalVideoActivity.class);
                break;
            case R.id.ivCollectVideoImg://收藏视频
                startActivity(CollectVideoActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        VideoAlbumActivity.albumId = albumAdapter.getItem(position).getId();
        startActivity(VideoAlbumActivity.class);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getCollectVideo();
        getAlbumData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            getPermission();
        }
    }

    private void getPermission() {
        StkPermissionHelper.reqManageExternalStoragePermission()
                .reqPermissionDesc(getString(R.string.get_video_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        mDataBinding.tvLocalVideoNum.setText(getString(R.string.no_permission));
                        mDataBinding.ivLocalVideoImg.setImageResource(R.drawable.atianjiawuzhuangt);
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.VIDEO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    Glide.with(mContext).load(selectMediaEntities.get(0).getPath()).into(mDataBinding.ivLocalVideoImg);
                    mDataBinding.tvLocalVideoNum.setText(getString(R.string.video_num, selectMediaEntities.size()));
                } else {
                    mDataBinding.ivLocalVideoImg.setImageResource(R.drawable.atianjiawuzhuangt);
                    mDataBinding.tvLocalVideoNum.setText(getString(R.string.video_num, 0));
                }
            }
        });
    }

    private void getCollectVideo() {
        List<SelectMediaEntity> listSp = AppSpUtils.getCollectList();
        if (listSp != null && listSp.size() > 0) {
            Glide.with(mContext).load(listSp.get(0).getPath()).into(mDataBinding.ivCollectVideoImg);
            mDataBinding.tvCollectVideoNum.setText(getString(R.string.video_num, listSp.size()));
        } else {
            mDataBinding.ivCollectVideoImg.setImageResource(R.drawable.atianjiawuzhuangt);
            mDataBinding.tvCollectVideoNum.setText(getString(R.string.video_num, 0));
        }
    }

    public void showAddAlbumDialog() {
        RenameDialog dialog = new RenameDialog(mContext);
        dialog.title = getString(R.string.create_video_album);
        dialog.hint = getString(R.string.input_video_album_name);
        dialog.setListener(new RenameDialog.Ilistener() {
            @Override
            public void clickRename(String name) {
                addAlbumData(name);
            }
        });
        dialog.show();
    }

    //添加相册数据
    private void addAlbumData(String name) {
        //准备数据
        List<MyAlbumBean> listSave = new ArrayList<>();
        MyAlbumBean bean = new MyAlbumBean(TimeUtils.getNowString(), name, new ArrayList<>());
        listSave.add(bean);
        //保存数据
        List<MyAlbumBean> listSp = AppSpUtils.getAlbumList();
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            AppSpUtils.saveAlbumList(listSp);
        } else {
            AppSpUtils.saveAlbumList(listSave);
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dismissDialog();
                getAlbumData();
            }
        }, 500);
    }

    //获取相册数据
    private void getAlbumData() {
        List<MyAlbumBean> listSp = AppSpUtils.getAlbumList();
        if (listSp != null && listSp.size() > 0) {
            albumAdapter.setList(listSp);
            mDataBinding.rvAlbumList.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.rvAlbumList.setVisibility(View.GONE);
        }
    }

}